package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
      log.info("业务层开始调用套餐的分页查询接口");
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<Setmeal> setmealPage= setmealMapper.pageQuery(setmealPageQueryDTO);
        return  new PageResult(setmealPage.getTotal(),setmealPage.getResult());
    }


    /**
     * 新增菜品
     * @param setmealDTO
     */
    @Override
    public void save(SetmealDTO setmealDTO) {
        log.info("Service层开始调用新增套餐接口：{}"  , setmealDTO);
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.saveSetmeal(setmeal);

        Long id=setmeal.getId();

        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
        if(!setmealDishes.isEmpty()){
            setmealDishes.forEach(setmealDish->{
                setmealDish.setSetmealId(id);
            });
        }
        setmealDishMapper.insertSetmealDishes(setmealDishes);
    }


    /**
     *根据id查套餐数据
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {

        log.info("开始根据id查询套餐:{}",id);
        Setmeal setmeal=setmealMapper.selectSetmealByid(id);
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);

        //获取套餐的id
        Long setmealId=setmeal.getId();

        List<SetmealDish> setmealDishes= setmealDishMapper.getDishIdBySetmealId(setmealId);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public void modify(SetmealDTO setmealDTO) {
        log.info("Service层开始修改套餐数据:{}",setmealDTO);

        //把DTO用entity封裝
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
    }

    @Override
    public void modifyStatus(String status, String id) {
        Setmeal setmeal = setmealMapper.selectSetmealByid(Long.valueOf(id));
        setmeal.setStatus(Integer.parseInt(status));
        setmealMapper.update(setmeal);
    }

    @Override
    public void delete(List<Long> ids) {
        log.info("业务层开始调用删除接口：{}",ids);
        //判断套餐是否处于启售状态
        for(Long id:ids){
            Integer status= Integer.valueOf(setmealMapper.selectSetmealByid(id).getStatus().toString());
            if(status.equals(StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //执行删除操作
        setmealMapper.delete(ids);
        setmealDishMapper.delete(ids);
    }




    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }


}
